package com.hd.sup.modules.alarm.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.hd.common.utils.Pair;
import com.hd.common.utils.ValueUtils;
import com.hd.sup.modules.alarm.mapper.AlarmDailyStatisMapper;
import com.hd.sup.modules.alarm.model.AlarmDailyStatis;
import com.hd.sup.modules.alarm.service.IAlarmDailyStatisService;
import com.hd.sup.modules.alarm.util.AlarmUtils;
import com.hd.sup.modules.alarm.util.TemplateUtils;
import com.hd.sup.modules.poi.model.EnterpriseAlarmExport;
import com.hd.sup.modules.poi.model.RegionAlarmExport;
import com.hd.sup.modules.vehicle.model.VehicleAlarmDailyStatis;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 报警基础数据表 | 企业编号 报警类型  统计时间 服务实现类
 * </p>
 *
 * @author chentl123
 * @since 2020-12-28
 */
@Service
public class AlarmDailyStatisServiceImpl extends ServiceImpl<AlarmDailyStatisMapper, AlarmDailyStatis> implements IAlarmDailyStatisService {

    @Override
    public Map<String, Object> getDailyAlarmTypeSum(String currentDateStr, List<String> deptIds) {
        return this.baseMapper.getDailyAlarmTypeSum(currentDateStr, deptIds);
    }

    @Override
    public List<Map<String, Object>> getTrendData(AlarmDailyStatis AlarmDailyStatis) {
        return this.baseMapper.getTrendData(AlarmDailyStatis);
    }

    @Override
    public List<Map<String, Object>> trendDataForReport(VehicleAlarmDailyStatis alarmDailyStatis) {
        return this.baseMapper.trendDataForReport(alarmDailyStatis);
    }

    @Override
    public List<Map<String, Object>> getHandleRateTop5(List<String> deptIds) {
        return this.baseMapper.getHandleRateTop5(deptIds);
    }


    @Override
    public Map<String, Object> getWorstEnterpriseHandleRateTop20(VehicleAlarmDailyStatis param, String timeFlag, List<String> deptIds) {
        Map<String, Object> res = Maps.newHashMap();
        Map<String, Object> templates = Maps.newHashMap();


        Pair<String, String> datePair = AlarmUtils.getStartEndDate(param.getDateStr(), timeFlag, false);
        param.setStartDateStr(datePair.first);
        param.setEndDateStr(datePair.second);

        //1 查询top20 处理率最慢企业
        List<Map<String, Object>> result = this.baseMapper.getWorstEnterpriseHandleRateTop20(param, deptIds);

        Map<String, Object> standardEnterpriseMap = this.baseMapper.getStandardEnterpriseCount(param, deptIds);

        int total = ValueUtils.parseInt(standardEnterpriseMap.get("total"));
        int standard = ValueUtils.parseInt(standardEnterpriseMap.get("standard"));
        String standard_rate = total == 0 ? "0" : ValueUtils.getRate(standard, total, 2);

        //2 生成处置率分析后的描述
        String desc = TemplateUtils.getEnterpriseRateDesc(timeFlag, param.getStartDateStr(), param.getEndDateStr(), standard, total - standard, standard_rate);
        templates.put("t1", desc);

        res.put("data", result);
        res.put("templates", templates);
        return res;
    }

    @Override
    public List<Map<String, Object>> getAlarmEnterprisePage(Page<Map<String, Object>> page, AlarmDailyStatis alarmDailyStatis) {
        return this.baseMapper.getAlarmEnterprisePage(page, alarmDailyStatis);
    }

    @Override
    public List<Map<String, Object>> getAlarmRegionPage(Page<Map<String, Object>> page, AlarmDailyStatis alarmDailyStatis) {
        return this.baseMapper.getAlarmRegionPage(page, alarmDailyStatis);
    }

    @Override
    public List<RegionAlarmExport> getAlarmRegionExport(Page<AlarmDailyStatis> page, AlarmDailyStatis alarmDailyStatis) {
        return this.baseMapper.getAlarmRegionExport(page, alarmDailyStatis);
    }

    @Override
    public List<EnterpriseAlarmExport> getAlarmEnterpriseExport(Page<AlarmDailyStatis> page, AlarmDailyStatis alarmDailyStatis) {
        return this.baseMapper.getAlarmEnterpriseExport(page, alarmDailyStatis);
    }

    @Override
    public List<Map<String, Object>> getRegionAlarmHanderRatePage(Page<Map<String, Object>> page, AlarmDailyStatis alarmDailyStatis) {
        return this.baseMapper.getRegionAlarmHanderRatePage(page, alarmDailyStatis);
    }

    @Override
    public List<Map<String, Object>> getEnterpriseAlarmHanderRatePage(Page<Map<String, Object>> page, AlarmDailyStatis alarmDailyStatis) {
        return this.baseMapper.getEnterpriseAlarmHanderRatePage(page, alarmDailyStatis);
    }

    @Override
    public List<AlarmDailyStatis> getTodayStatisData(String currentDate) {
        return this.baseMapper.getTodayStatisData(currentDate);
    }

    @Override
    public void refreshAlarmDailyStatis(AlarmDailyStatis alarmDailyStatis) {
        this.baseMapper.refreshAlarmDailyStatis(alarmDailyStatis);
    }
}
